package com.lcc.control;

import com.lcc.pojo.Book;
import com.lcc.pojo.PageBean;
import com.lcc.pojo.Query;
import com.lcc.service.BookService;
import com.lcc.service.ImgUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author 廖聪聪
 * @version 1.0
 * 创建时间 2020/7/23 11:00
 */
@Controller
@RequestMapping("/book")
public class BookControl {
	//注入service
	@Autowired
	private BookService bookService;
	@Autowired
	private ImgUpService imgUpService;

	/**
	 * 分页查询
	 * @param model
	 * @param query
	 * @param pg
	 * @return
	 */
	@RequestMapping(value = "findallbook")
	public String findAll(Model model, Query query,String pg){
		PageBean pageBean = new PageBean();
		List<Book> bookList= bookService.findByBook(query,pageBean,pg);
		model.addAttribute("bookList",bookList);
		model.addAttribute("pageBean",pageBean);
		return "listshow";
	}

	/**
	 * 根据id查询某本书
	 * @param bookid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findByid")
	public String findByidBook(Integer bookid,Model model,HttpServletRequest request){
		Book book= bookService.findByidBook(bookid);
		request.getSession().setAttribute("bookid",bookid);
		model.addAttribute("book",book);
		return "search";
	}

	/**
	 * 根据id删除书籍
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deletbyid")
	public String deleteBook(String[] id){
		boolean flag= bookService.deletBookByid(id);
		//若删除成功，进行页面跳转
		if (flag){
			return "redirect:findallbook";
		}
		return "error";
	}

	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping(value = "doaddpage")
	public String goadd(){
		return "addBook";
	}

	@InitBinder
	public void init(WebDataBinder webDataBinder){
		//指定什么格式，前台传什么格式
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat.setLenient(false);
		webDataBinder.registerCustomEditor(Date.class,new CustomDateEditor(simpleDateFormat,false));
	}

	/**
	 * 添加书籍，并且图片上传
	 * @param
	 * @return
	 */
	@RequestMapping(value = "addBook",method = RequestMethod.POST)
	public String addBook(Book book, @RequestParam MultipartFile pic,HttpServletRequest request){
		OutputStream os = null;
		try {
		    String uuid = UUID.randomUUID().toString();
		    File file = new File("E:/tomcat8.0/webapps/img/"+uuid+pic.getOriginalFilename());
			os = new FileOutputStream(file);
			FileCopyUtils.copy(pic.getInputStream(),os);
			book.setImg_path("http://localhost:8080/img/"+uuid+pic.getOriginalFilename());
			boolean b = bookService.addBook(book);
			if (b){
				return "redirect:findallbook";
			}else {
				return "error";
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
